﻿#region License


// Virtual Essentials - http://www.Virtual-Essentials.com
// Copyright (c) 2005-2013
// 
//  This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Definitions;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;

using VirtualEssentials.DNN.Modules.Testimonials.Controller;

namespace VirtualEssentials.DNN.Modules.Testimonials.Components
{
    class TestimonialBusinessController : IUpgradeable, IPortable,ISearchable 
    {
        DataProvider data = new DataProvider();

        #region Optional Interfaces
        public string UpgradeModule(string version)
        {
            switch (version)
            {
                case "01.00.00":
                    InitModulePermissions();
                    break;                
            }

            return version;
        }

        private void InitModulePermissions()
        {
           
            PermissionController objPermController = new PermissionController();
            //Get the Module Definition ID
            DesktopModuleInfo objDesktopModInfo = DesktopModuleController.GetDesktopModuleByModuleName("veTestimonials", DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings().PortalId);
            ModuleDefinitionInfo objModDefInfo = ModuleDefinitionController.GetModuleDefinitionByFriendlyName("veTestimonials", objDesktopModInfo.DesktopModuleID);

            try
            {
                PermissionInfo objPermInfo = new PermissionInfo();
                objPermInfo.ModuleDefID = objModDefInfo.ModuleDefID;


                if (objPermController.GetPermissionsByModuleDefID(objModDefInfo.ModuleDefID) == null)
                {
                    objPermInfo.PermissionCode = "VE_TESTIMONIALS";
                    objPermInfo.PermissionKey = "ADDTESTIMONIAL";
                    objPermInfo.PermissionName = "Add Testimonials";
                    objPermController.AddPermission(objPermInfo);               

                    objPermInfo = new PermissionInfo();
                    objPermInfo.ModuleDefID = objModDefInfo.ModuleDefID;
                    objPermInfo.PermissionCode = "VE_TESTIMONIALS";
                    objPermInfo.PermissionKey = "VIEWTESTIMONIAL";
                    objPermInfo.PermissionName = "View Testimonials";
                    objPermController.AddPermission(objPermInfo);

                    objPermInfo = new PermissionInfo();
                    objPermInfo.ModuleDefID = objModDefInfo.ModuleDefID;
                    objPermInfo.PermissionCode = "VE_TESTIMONIALS";
                    objPermInfo.PermissionKey = "PUBLISHTESTIMONIAL";
                    objPermInfo.PermissionName = "Publish Testimonials";
                    objPermController.AddPermission(objPermInfo);
                }

            }
            catch { }
        }

        /// <summary>
        /// Integrates searching interface to allow testimonials to be searchable
        /// </summary>
        /// <param name="ModInfo"></param>
        /// <returns></returns>
        public SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {
        //TestimonialController objController = new TestimonialController();            
	    List<TestimonialInfo> colTestimonials = (List<TestimonialInfo>)data.GetTestimonials(ModInfo.ModuleID);
	    SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

        foreach (TestimonialInfo item in colTestimonials)
        {
		SearchItemInfo SearchItem = default(SearchItemInfo);
        DateTime pubDate = Convert.ToDateTime(item.TestimonialDate);
        SearchItem = new SearchItemInfo(ModInfo.ModuleTitle + " - User Submitted Testimonial", 
            item.Testimonial,
            0,
            pubDate,
            ModInfo.ModuleID, 
            item.TestimonialID.ToString(),
            String.Format("{0} Submitted By:{1} {2} {6} {3} {4} {7}{5}", item.Testimonial, item.AuthorFirstName, item.AuthorLastName, item.AuthorTitle, item.AuthorLocation, item.AuthorWebsite, string.IsNullOrEmpty(item.AuthorTitle)? "" : ", ", string.IsNullOrEmpty(item.AuthorWebsite) ? "" : "Website:")
            );
		SearchItemCollection.Add(SearchItem);
	}

	return SearchItemCollection;

        }

   
        /// <summary>
        /// Exports the content from one module to another
        /// </summary>
        /// <param name="ModuleId"></param>
        /// <returns></returns>
        public string ExportModule(int ModuleId)
        {
            //TestimonialController objController = new TestimonialController();
            StringBuilder sb = new StringBuilder();
            List<TestimonialInfo> colInfo = (List<TestimonialInfo>)data.GetTestimonials(ModuleId);

            if (colInfo.Count > 0)
            {
                sb.Append("<Testimonials>");
                foreach (TestimonialInfo item in colInfo)
                {
                    sb.Append("<TestimonialItem>");
                    sb.Append("<TestimonialID>");
                    sb.Append(XmlUtils.XMLEncode(item.TestimonialID.ToString()));
                    sb.Append("</TestimonialID>");
                    sb.Append("<Testimonial>");
                    sb.Append(XmlUtils.XMLEncode(item.Testimonial));
                    sb.Append("</Testimonial>");
                    sb.Append("<AuthorFirstName>");
                    sb.Append(XmlUtils.XMLEncode(item.AuthorFirstName));
                    sb.Append("</AuthorFirstName>");
                    sb.Append("<AuthorLastName>");
                    sb.Append(XmlUtils.XMLEncode(item.AuthorLastName));
                    sb.Append("</AuthorLastName>");
                    sb.Append("<AuthorLocation>");
                    sb.Append(XmlUtils.XMLEncode(item.AuthorLocation));
                    sb.Append("</AuthorLocation>");
                    sb.Append("<IsPublished>");
                    sb.Append(XmlUtils.XMLEncode(item.IsPublished.ToString()));
                    sb.Append("</IsPublished>");
                    sb.Append("<TestimonialDate>");
                    sb.Append(XmlUtils.XMLEncode(item.TestimonialDate));
                    sb.Append("</TestimonialDate>");
                    sb.Append("<AuthorTitle>");
                    sb.Append(XmlUtils.XMLEncode(item.AuthorTitle));
                    sb.Append("</AuthorTitle>");
                    sb.Append("<AuthorWebsite>");
                    sb.Append(XmlUtils.XMLEncode(item.AuthorWebsite));
                    sb.Append("</AuthorWebsite>");
                    sb.Append("</TestimonialItem>");
                }
                sb.Append("</Testimonials>");
            }
            return sb.ToString();
        }

        /// <summary>
        /// Imports a collection of module items into a new moduel instance
        /// </summary>
        /// <param name="ModuleId"></param>
        /// <param name="Content"></param>
        /// <param name="Version"></param>
        /// <param name="UserID"></param>
        public void ImportModule(int ModuleId, string Content, string Version, int UserID)
        {

           // TestimonialController objController = new TestimonialController();
            //allows to do something specific based on the version being imported
            if (int.Parse(Version.Substring(0, 2)) >= 1)
            {

            }

            XmlNode colItems = DotNetNuke.Common.Globals.GetContent(Content, "Testimonials");

            foreach (XmlNode item in colItems.SelectNodes("TestimonialItem"))
            {
                TestimonialInfo objItem = new TestimonialInfo();
                //objItem.TestimonialID = Int32.Parse(item.SelectSingleNode("TestimonialID").InnerText);
                objItem.Testimonial = item.SelectSingleNode("Testimonial").InnerText;
                objItem.AuthorFirstName = item.SelectSingleNode("AuthorFirstName").InnerText;
                objItem.AuthorLastName = item.SelectSingleNode("AuthorLastName").InnerText;
                objItem.AuthorLocation = item.SelectSingleNode("AuthorLocation").InnerText;
                objItem.IsPublished = bool.Parse(item.SelectSingleNode("IsPublished").InnerText);
                objItem.TestimonialDate = item.SelectSingleNode("TestimonialDate").InnerText;
                objItem.AuthorTitle = item.SelectSingleNode("AuthorTitle").InnerText;
                objItem.AuthorWebsite = item.SelectSingleNode("AuthorWebsite").InnerText;
                objItem.ModuleId = ModuleId;
                data.AddNewTestimonial(objItem);

                
                //add to the database
                //objController.ImportTestimonial(objItem.Testimonial, objItem.AuthorFirstName, objItem.AuthorLastName, objItem.AuthorLocation, ModuleId, objItem.IsPublished, objItem.TestimonialDate, objItem.AuthorTitle, objItem.AuthorWebsite);
            }
        }
        #endregion
    }
}
